package com.falm.picturebackend.controller;

import com.falm.picturebackend.common.BaseResponse;
import com.falm.picturebackend.common.ResultUtils;
import com.falm.picturebackend.entity.BO.FilesBO;
import com.falm.picturebackend.entity.DTO.FileAddDTO;
import com.falm.picturebackend.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/file")
@Tag(name = "文件接口")
public class FileController {

    @Resource
    private FileService fileService;

    @Operation(summary = "上传文件")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile file,
        @RequestPart(value = "parentFilePath", required = false) String parentFilePath) throws Exception {
        FileAddDTO fileAddDTO = new FileAddDTO();
        fileAddDTO.setFile(file);
        fileAddDTO.setParentFilePath(parentFilePath);
        fileService.uploadFile(fileAddDTO);
        return ResultUtils.success();
    }

    @Operation(summary = "删除文件")
    @PostMapping("/delete")
    public BaseResponse<Void> deleteFile(@RequestBody String filePath) {
        fileService.deleteFile(filePath);
        return ResultUtils.success();
    }

    @Operation(summary = "获取文件列表")
    @GetMapping("/list")
    public BaseResponse<List<FilesBO>> listFiles() {
        return ResultUtils.success(fileService.getAllFileS());
    }
}
